package com.theeviljames.coursework;

import java.io.File;
import java.util.Hashtable;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.theeviljames.coursework.ga.ANNGa;
import com.theeviljames.coursework.problems.FourBitParity;
import com.theeviljames.coursework.problems.ThreeBitParity;

public class GATester {

	private static final int[] popSizes = new int[]{
		10,20,50,100,200,
	};
	private static final double[] mutateRates = new double[]{
		0.05,0.1,0.2,0.3,
	};
	//Sum of squares error over all training examples
	private static final double errorGoal = 0.1;
	private static final int maxGenerations = 10000;
	private static final int noOfElite = 2;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			for(int popSize:popSizes){
				
			WritableWorkbook workBook = Workbook.createWorkbook(new File("GAfourbitparity"+ popSize + ".xls"));
			int counter = 0;
				for(double mutateRate:mutateRates){
					ANNGa ga = new ANNGa(popSize,maxGenerations,new int[]{4,4,2},mutateRate,noOfElite, new FourBitParity());
					ga.run();
					Hashtable<Integer,Double> bestError = ga.getBestError();
					Hashtable<Integer,Double> totalError = ga.getSumError();
					String sheetname = "P" + popSize + " M" + mutateRate;
					WritableSheet sheet = workBook.createSheet(sheetname, counter++);
					Label blabel = new Label(1,0,"Best Error");
					Label slabel = new Label(2,0,"Sum Error");
					sheet.addCell(blabel);
					sheet.addCell(slabel);
					//Add the labels
					for(int i = 0; i <= maxGenerations; i++){
						Label l = new Label(0,i+1,""+i);
						sheet.addCell(l);
					}
					for(int i = 0; i <= maxGenerations; i++){
						if(bestError.get(i)==null)break;
						Number b = new Number(1,i+1,bestError.get(i));
						sheet.addCell(b);
					}
					for(int i = 0; i <= maxGenerations; i++){
						if(totalError.get(i)==null)break;
						Number b = new Number(2,i+1,totalError.get(i));
						sheet.addCell(b);
					}
				}

				workBook.write();
				workBook.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

}
